package cn.tedu.coolshark.controller;

import cn.tedu.coolshark.mapper.CategoryMapper;
import cn.tedu.coolshark.pojo.entity.Category;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Justin Parker
 * @author 2024/8/22 22:56
 * @version 1.0
 */
@Slf4j
@Api(tags = "分类模块管理")
@RestController
@RequestMapping("/v1/categories")
public class CategoryController {

    @Autowired
    private CategoryMapper categoryMapper;

    @ApiOperation(value = "查询所有分类")
    @PostMapping("/select")
    public List<Category> select(){
        return categoryMapper.select();
    }

    @ApiOperation(value = "根据id删除分类")
    @PostMapping("/deleteById")
    public String deleteById(Integer id){
      Boolean result = categoryMapper.deleteById(id);
        if (!result){
            return "删除失败";
        }
            return "删除成功";
    }

    @ApiOperation(value = "添加分类")
    @PostMapping("/insert")
    public String insert(String name){
    Boolean result = categoryMapper.insert(name);
    if (!result){
        return "添加失败";
    }
        return "添加成功";
    }


    @ApiOperation(value = "修改分类")
    @PostMapping("/update")
    public String update(@RequestBody Category category){
    Boolean result =categoryMapper.update(category);
        if (!result){
            return "修改失败";
        }
        return "修改成功";
    }
}
